package com.gitee.wsl.doc.prosemirror.transform.ext

import com.gitee.wsl.doc.prosemirror.model.content.ContentMatch
import com.gitee.wsl.doc.prosemirror.model.content.ResolvedPos
import com.gitee.wsl.doc.prosemirror.model.fragment.Fragment
import com.gitee.wsl.doc.prosemirror.model.fragment.ext.isInvalidMarks
import com.gitee.wsl.doc.prosemirror.model.node.NodeType

fun contentAfterFits(_to: ResolvedPos, depth: Int, type: NodeType, match: ContentMatch?, open: Boolean): Fragment? {
    val node = _to.node(depth)
    val index = if (open) _to.indexAfter(depth) else _to.index(depth)
    if (index == node.childCount && !type.compatibleContent(node.type)) return null
    val fit = match?.fillBefore(node.content, true, index)
    return if (fit != null && !node.content.isInvalidMarks(type, index)) fit else null
}